package cn.gson.boot.model.service;

import cn.gson.boot.model.dao.ContactsDao;
import cn.gson.boot.model.mapper.ContactsMapper;
import cn.gson.boot.model.mapper.KehuMapper;
import cn.gson.boot.model.pojos.Contacts;
import cn.gson.boot.model.pojos.Kehu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ContactsService {
    @Autowired
    ContactsDao contactsDao;//联系人dao

    @Autowired
    KehuMapper kehuMapper;//客户mapper

    @Autowired
    ContactsMapper contactsMapper;//联系人mapper

    //模糊查询和分页查询
    public List<Contacts> allContacts(String cName,String cSex,int empId){
        return contactsMapper.allContacts(cName,cSex,empId);
    }

    //详情查询
    public Contacts xqContacts(Integer cId){
        return contactsMapper.quanContacts(cId);
    }

    //新增和修改
    public String  addContacts(Contacts contacts){
        int id = contacts.getcId();
        Date date = new  Date();
        Timestamp tt = new Timestamp(date.getTime());
        if(id==0){
            int phoneshu = contactsDao.findKehuByMphone2(contacts.getcMphone().toString(),contacts.getcWphone().toString());
            if (phoneshu >0){
                return "联系人手机号重复！";
            }
            System.out.println("开始新增");
            contacts.setcCjjian(tt);
            contacts.setcZtai(1);
            contactsDao.save(contacts);
        }else{
            System.out.println("开始修改");
            Contacts c2 = contactsDao.findById(id).get();//找到修改id
            c2.setcGxjian(tt);
            c2.setcName(contacts.getcName());c2.setcMphone(contacts.getcMphone());
            c2.setcFenl(contacts.getcFenl());c2.setcSex(contacts.getcSex());c2.setcFzyw(contacts.getcFzyw());
            c2.setcWphone(contacts.getcWphone());c2.setcYoujdizhi(contacts.getcYoujdizhi());
            c2.setcPhone(contacts.getcPhone());c2.setcJphone(contacts.getcJphone());
            c2.setcYoub(contacts.getcYoub());c2.setcHomed(contacts.getcHomed());c2.setcCzhen(contacts.getcCzhen());
            c2.setcWX(contacts.getcWX());c2.setcQQ(contacts.getcQQ());c2.setcWXnc(contacts.getcWXnc());
            c2.setcQQnc(contacts.getcQQnc());c2.setcWangw(contacts.getcWangw());
            c2.setkId(contacts.getkId());c2.setcSheng(contacts.getcSheng());
            c2.setcShi(contacts.getcShi());c2.setcQu(contacts.getcQu());
            System.out.println("省："+contacts.getcSheng());
            System.out.println("市："+contacts.getcShi());
            System.out.println("区："+contacts.getcQu());
            contactsDao.save(c2);
        }
        return "success";
    }

    //逻辑删除
    public void deleteContacts(int id){
        Contacts c1 = contactsDao.findById(id).get();
        c1.setcZtai(0);
        contactsDao.save(c1);
    }

}
